package cn.edu.scau.cmi.wusonglang.orm.dao;


import java.util.List;
import java.util.Set;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

import cn.edu.scau.cmi.wusonglang.orm.entity.Student;
import cn.edu.scau.cmi.wusonglang.orm.entity.Tutor;

public class TutorDAO {

    private  Configuration configuration;

    private  SessionFactory factory;

    private  Session session;

  

    public Tutor findTeacherById(Integer id) {
        session = factory.openSession();
        Transaction transaction = session.getTransaction();
        transaction.begin();
        Tutor teacher = session.get(Tutor.class, id);
        transaction.commit();
        session.close();
        configuration = new Configuration();
        //configuration.configure().addClass(User.class);
        configuration.configure("classpath:./hibernate.cfg.xml");
        factory = configuration.buildSessionFactory();
        return teacher;
    }

    public Tutor findTeacherByName(String teacherName){
    	 session = factory.openSession();
         Transaction transaction = session.getTransaction();
         transaction.begin();
         Query query = session.createQuery("FROM Tutor Where name = ?1");
         query.setParameter(1,teacherName);
         List<Tutor> list = query.list();
         transaction.commit();
         session.close();
         return list.get(0);
    }

    public void newTeacher(Tutor teacher){
        session = factory.openSession();
        Transaction transaction = session.getTransaction();
        transaction.begin();
        session.save(teacher);
        transaction.commit();
        session.close();
    }

    public void updateTeacher(Tutor teacher){
        session = factory.openSession();
        Transaction transaction = session.getTransaction();
        transaction.begin();
        session.update(teacher);
        transaction.commit();
        session.close();
    }

    public List<Tutor> findAllTeacher(){
        session = factory.openSession();
        Transaction transaction = session.getTransaction();
        transaction.begin();
        Query query = session.createQuery("FROM Tutor");
        List<Tutor> list = query.list();
        transaction.commit();
        session.close();
        return list;
    }

    public void deleteTeacher(Tutor teacher){
        StudentDAO studentDAO = new StudentDAO();
        Set<Student> studentSet = teacher.getStudentSet();
        for(Student student : studentSet){
            student.setTutor(null);
            studentDAO.updateStudent(student);
        }
        teacher.setStudentSet(null);
        session = factory.openSession();
        Transaction transaction = session.getTransaction();
        transaction.begin();
        session.delete(teacher);
        transaction.commit();
        session.close();
    }
}
